<?php 

/**
 * 登录用户主页（与用户展示主页不同，需要登录验证）
 * @author user
 *
 */
class HomeController extends SCController {
	
	public function __construct() {
		parent::__construct();
	}
	
	public function index() {
		($user_id = SCScabish::model('user')->checkLogin()) || $this->redirect->to('login');
		
		$user_info = SCScabish::model('user')->getBasicInfo($user_id);
		
		$things = $this->model->from('sc_thing')->where('user_id = '.$user_id)->order('post_date DESC')->limit(10)->find();
		foreach($things as $key=>$thing) {
			if($thing->type == 'photo') {
				$photos = $this->model->reset()->from('sc_photo')->where('thing_id = '.$thing->thing_id)->order('photo_id DESC')->find();
				foreach($photos as $k=>$photo) {
					$photos[$k]->thumbs = unserialize($photo->thumbs);
				}
				$thing->photos = $photos;
			} elseif($thing->type == 'video') {
				$thing->video = $this->model->reset()->from('sc_video')->where('thing_id = '.$thing->thing_id)->findOne();
			} elseif($thing->type == 'music') {
				$thing->music = $this->model->reset()->from('sc_music')->where('thing_id = '.$thing->thing_id)->findOne();
			}
			$thing->comment_num = $this->model->reset()->field('COUNT(*)')->from('sc_comment')->where('thing_id = '.$thing->thing_id)->findScalar();
		}
		require $this->view->load();
	}
	
	/**
	 * 
	 * 获取更早之前发布的thing
	 * $id 分界点ID 
	 */
	public function getThings() {
		$id = !is_null(SCRequest::get('id')) ? SCRequest::get('id') : die('oops!');
		$user_id = !is_null(SCRequest::get('uid')) ? SCRequest::get('uid') : 0;
		
		$ThingModel = new ThingModel();
		$this->ajaxSuccess($ThingModel->getThings($id, $user_id));
	}
	
}